<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>KMP.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="KMP code in Java">
<meta NAME="TITLE" CONTENT="KMP code in Java">
<meta NAME="KEYWORDS" CONTENT="KMP,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>KMP.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "KMP.java">KMP.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac KMP.java</span>
<span class="comment"> *  Execution:    java KMP pattern text</span>
<span class="comment"> *  Dependencies: StdOut.java</span>
<span class="comment"> *</span>
<span class="comment"> *  Reads in two strings, the pattern and the input text, and</span>
<span class="comment"> *  searches for the pattern in the input text using the</span>
<span class="comment"> *  KMP algorithm.</span>
<span class="comment"> *</span>
<span class="comment"> *  % java KMP abracadabra abacadabrabracabracadabrabrabracad</span>
<span class="comment"> *  text:    abacadabrabracabracadabrabrabracad </span>
<span class="comment"> *  pattern:               abracadabra          </span>
<span class="comment"> *</span>
<span class="comment"> *  % java KMP rab abacadabrabracabracadabrabrabracad</span>
<span class="comment"> *  text:    abacadabrabracabracadabrabrabracad </span>
<span class="comment"> *  pattern:         rab</span>
<span class="comment"> *</span>
<span class="comment"> *  % java KMP bcara abacadabrabracabracadabrabrabracad</span>
<span class="comment"> *  text:    abacadabrabracabracadabrabrabracad </span>
<span class="comment"> *  pattern:                                   bcara</span>
<span class="comment"> *</span>
<span class="comment"> *  % java KMP rabrabracad abacadabrabracabracadabrabrabracad </span>
<span class="comment"> *  text:    abacadabrabracabracadabrabrabracad</span>
<span class="comment"> *  pattern:                        rabrabracad</span>
<span class="comment"> *</span>
<span class="comment"> *  % java KMP abacad abacadabrabracabracadabrabrabracad</span>
<span class="comment"> *  text:    abacadabrabracabracadabrabrabracad</span>
<span class="comment"> *  pattern: abacad</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">KMP</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class finds the first occurrence of a pattern string</span>
<span class="comment"> *  in a text string.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses a version of the Knuth-Morris-Pratt substring search</span>
<span class="comment"> *  algorithm. The version takes time as space proportional to</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> + </span><span class="keyword">&lt;em&gt;</span><span class="comment">M R</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> in the worst case, where </span><span class="keyword">&lt;em&gt;</span><span class="comment">N</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the length</span>
<span class="comment"> *  of the text string, </span><span class="keyword">&lt;em&gt;</span><span class="comment">M</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the length of the pattern, and </span><span class="keyword">&lt;em&gt;</span><span class="comment">R</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  is the alphabet size.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/53substring"</span><span class="keyword">&gt;</span><span class="comment">Section 5.3</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">KMP</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> R</span><span class="symbol">;</span><span class="normal">       </span><span class="comment">// the radix</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[][]</span><span class="normal"> dfa</span><span class="symbol">;</span><span class="normal">       </span><span class="comment">// the KMP automoton</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">char</span><span class="symbol">[]</span><span class="normal"> pattern</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// either the character array for the pattern</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> pat</span><span class="symbol">;</span><span class="normal">        </span><span class="comment">// or the pattern string</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Preprocesses the pattern string.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> pat the pattern string</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">KMP</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> pat</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">R </span><span class="symbol">=</span><span class="normal"> </span><span class="number">256</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">pat </span><span class="symbol">=</span><span class="normal"> pat</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// build DFA from pattern</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> M </span><span class="symbol">=</span><span class="normal"> pat</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span>
<span class="normal">        dfa </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">R</span><span class="symbol">][</span><span class="normal">M</span><span class="symbol">];</span><span class="normal"> </span>
<span class="normal">        dfa</span><span class="symbol">[</span><span class="normal">pat</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">)][</span><span class="number">0</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> </span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> X </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> c </span><span class="symbol">&lt;</span><span class="normal"> R</span><span class="symbol">;</span><span class="normal"> c</span><span class="symbol">++)</span><span class="normal"> </span>
<span class="normal">                dfa</span><span class="symbol">[</span><span class="normal">c</span><span class="symbol">][</span><span class="normal">j</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> dfa</span><span class="symbol">[</span><span class="normal">c</span><span class="symbol">][</span><span class="normal">X</span><span class="symbol">];</span><span class="normal">     </span><span class="comment">// Copy mismatch cases. </span>
<span class="normal">            dfa</span><span class="symbol">[</span><span class="normal">pat</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">j</span><span class="symbol">)][</span><span class="normal">j</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> j</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span><span class="normal">   </span><span class="comment">// Set match case. </span>
<span class="normal">            X </span><span class="symbol">=</span><span class="normal"> dfa</span><span class="symbol">[</span><span class="normal">pat</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">j</span><span class="symbol">)][</span><span class="normal">X</span><span class="symbol">];</span><span class="normal">     </span><span class="comment">// Update restart state. </span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal"> </span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Preprocesses the pattern string.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> pattern the pattern string</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> R the alphabet size</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">KMP</span><span class="symbol">(</span><span class="type">char</span><span class="symbol">[]</span><span class="normal"> pattern</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> R</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">R </span><span class="symbol">=</span><span class="normal"> R</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">pattern </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">char</span><span class="symbol">[</span><span class="normal">pattern</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> pattern</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span>
<span class="normal">            </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">pattern</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> pattern</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">];</span>

<span class="normal">        </span><span class="comment">// build DFA from pattern</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> M </span><span class="symbol">=</span><span class="normal"> pattern</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        dfa </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">R</span><span class="symbol">][</span><span class="normal">M</span><span class="symbol">];</span><span class="normal"> </span>
<span class="normal">        dfa</span><span class="symbol">[</span><span class="normal">pattern</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]][</span><span class="number">0</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> </span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> X </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal"> j</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> c </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> c </span><span class="symbol">&lt;</span><span class="normal"> R</span><span class="symbol">;</span><span class="normal"> c</span><span class="symbol">++)</span><span class="normal"> </span>
<span class="normal">                dfa</span><span class="symbol">[</span><span class="normal">c</span><span class="symbol">][</span><span class="normal">j</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> dfa</span><span class="symbol">[</span><span class="normal">c</span><span class="symbol">][</span><span class="normal">X</span><span class="symbol">];</span><span class="normal">     </span><span class="comment">// Copy mismatch cases. </span>
<span class="normal">            dfa</span><span class="symbol">[</span><span class="normal">pattern</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">]][</span><span class="normal">j</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> j</span><span class="symbol">+</span><span class="number">1</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// Set match case. </span>
<span class="normal">            X </span><span class="symbol">=</span><span class="normal"> dfa</span><span class="symbol">[</span><span class="normal">pattern</span><span class="symbol">[</span><span class="normal">j</span><span class="symbol">]][</span><span class="normal">X</span><span class="symbol">];</span><span class="normal">        </span><span class="comment">// Update restart state. </span>
<span class="normal">        </span><span class="cbracket">}</span><span class="normal"> </span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal"> </span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the index of the first occurrrence of the pattern string</span>
<span class="comment">     * in the text string.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  txt the text string</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the index of the first occurrence of the pattern string</span>
<span class="comment">     *         in the text string; N if no such match</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">search</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> txt</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// simulate operation of DFA on text</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> M </span><span class="symbol">=</span><span class="normal"> pat</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> txt</span><span class="symbol">.</span><span class="function">length</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> i</span><span class="symbol">,</span><span class="normal"> j</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N </span><span class="symbol">&amp;&amp;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            j </span><span class="symbol">=</span><span class="normal"> dfa</span><span class="symbol">[</span><span class="normal">txt</span><span class="symbol">.</span><span class="function">charAt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">)][</span><span class="normal">j</span><span class="symbol">];</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">j </span><span class="symbol">==</span><span class="normal"> M</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> i </span><span class="symbol">-</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// found</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal">                    </span><span class="comment">// not found</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the index of the first occurrrence of the pattern string</span>
<span class="comment">     * in the text string.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  text the text string</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the index of the first occurrence of the pattern string</span>
<span class="comment">     *         in the text string; N if no such match</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">search</span><span class="symbol">(</span><span class="type">char</span><span class="symbol">[]</span><span class="normal"> text</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// simulate operation of DFA on text</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> M </span><span class="symbol">=</span><span class="normal"> pattern</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> text</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> i</span><span class="symbol">,</span><span class="normal"> j</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> j </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N </span><span class="symbol">&amp;&amp;</span><span class="normal"> j </span><span class="symbol">&lt;</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            j </span><span class="symbol">=</span><span class="normal"> dfa</span><span class="symbol">[</span><span class="normal">text</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]][</span><span class="normal">j</span><span class="symbol">];</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">j </span><span class="symbol">==</span><span class="normal"> M</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> i </span><span class="symbol">-</span><span class="normal"> M</span><span class="symbol">;</span><span class="normal">    </span><span class="comment">// found</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal">                    </span><span class="comment">// not found</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/** </span>
<span class="comment">     * Takes a pattern string and an input string as command-line arguments;</span>
<span class="comment">     * searches for the pattern string in the text string; and prints</span>
<span class="comment">     * the first occurrence of the pattern string in the text string.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> pat </span><span class="symbol">=</span><span class="normal"> args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">];</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> txt </span><span class="symbol">=</span><span class="normal"> args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">];</span>
<span class="normal">        </span><span class="type">char</span><span class="symbol">[]</span><span class="normal"> pattern </span><span class="symbol">=</span><span class="normal"> pat</span><span class="symbol">.</span><span class="function">toCharArray</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">char</span><span class="symbol">[]</span><span class="normal"> text    </span><span class="symbol">=</span><span class="normal"> txt</span><span class="symbol">.</span><span class="function">toCharArray</span><span class="symbol">();</span>

<span class="normal">        </span><span class="usertype">KMP</span><span class="normal"> kmp1 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">KMP</span><span class="symbol">(</span><span class="normal">pat</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> offset1 </span><span class="symbol">=</span><span class="normal"> kmp1</span><span class="symbol">.</span><span class="function">search</span><span class="symbol">(</span><span class="normal">txt</span><span class="symbol">);</span>

<span class="normal">        </span><span class="usertype">KMP</span><span class="normal"> kmp2 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">KMP</span><span class="symbol">(</span><span class="normal">pattern</span><span class="symbol">,</span><span class="normal"> </span><span class="number">256</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> offset2 </span><span class="symbol">=</span><span class="normal"> kmp2</span><span class="symbol">.</span><span class="function">search</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// print results</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"text:    "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> txt</span><span class="symbol">);</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">"pattern: "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> offset1</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">" "</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">pat</span><span class="symbol">);</span>

<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">"pattern: "</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> offset2</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">" "</span><span class="symbol">);</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="normal">pat</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
